<template> <!-- 发货记录-->
	<view class="wrap">
		<js-error mode="bottom" ref="jsError"></js-error>
		<view class="search">
			<u-search @click="show = true" :show-action="false" placeholder="搜索" :disabled="true"></u-search>
		</view>
		<view>
			<u-popup v-model="show" mode="right" length="90%">
				<!-- <view class="text-blue padding-left-sm padding-top-sm" style="font-size: 18px;">
					<u-icon name="/static/common/img/find.png" size="64"></u-icon>
					<text class=" padding-left-sm text-blue">查询条件</text>
				</view> -->
				<view  style="font-size: 18px;border-bottom: 1px solid #aaa;font-weight: bold;padding: 10px;display: flex;justify-content: space-between;">
					<!-- <u-icon name="/static/image/ss.png" size="65"></u-icon> -->
					<text class=" padding-left-sm" style="color: #3E97B0;">筛选</text>
					<text @click="show=false">关闭</text>
				</view>
				<u-form class="form" style="padding: 0 10px;" :model="query" ref="uForm" label-position="left">
					<u-form-item class="text-bold" label="发货单号:" prop="dlid.cdlcode" label-width="200">
						<u-input placeholder="请输入发货单号" v-model="query.dlid.cdlcode" type="text"
							maxlength="200"></u-input>
					</u-form-item>
					<u-form-item class="text-bold" label="开始日期" prop="time" label-width="200">
						<u-input placeholder="请输入开始日期" v-model="query.ddate_gte" type="select" :select-open="startTime"
							@click="startTime = true"></u-input>
					</u-form-item>
					<u-form-item class="text-bold" label="结束日期" prop="time" label-width="200">
						<u-input placeholder="请输入结束日期" v-model="query.ddate_lte" type="select" :select-open="endTime"
							@click="endTime = true"></u-input>
					</u-form-item>
					<u-form-item class="text-bold" label="客户:" prop="dlid.ccusname" label-width="200">
						<u-input placeholder="请输入客户" v-model="query.dlid.ccusname" type="text"
							maxlength="200"></u-input>
					</u-form-item>
					<u-picker mode="time" v-model="startTime" @confirm="startConfirm"></u-picker>
					<u-picker mode="time" v-model="endTime" @confirm="endConfirm"></u-picker>
				</u-form>
				<!-- <view class="footer">
					<u-button class="btn" type="primary" @click="submit">查询</u-button>
					<u-button class="btn margin-top-lg" @click="reset">重置</u-button>
				</view> -->
				<view class=" cu-bar tabbar bg-white foot cu-modal-footer">
					
					<!-- round -->
					<button class="cu-btn  lines-red lg" @click="reset">重置</button>
					<button class="cu-btn  bg-confirm lg margin-left " @click="submit">查询</button>
				</view>
			</u-popup>
		</view>
		<scroll-view class="scroll-list" scroll-y="true" @scrolltolower="loadMore" style="padding: 10px;">
			<view v-for="(item, index) in fhList" :key="index" class=" bg-white" style="position: relative;border-radius: 10px;margin-bottom: 10px;border: 1px solid #eee;overflow: hidden;">

				<view class="cu-form-group" style="display: flex;justify-content: space-between;">
					
					<view class="">
						<view
							style=" display: inline-block;padding: 5px;background: #3E97B0;color: #fff;min-width: 40px;text-align: center;font-style: italic;font-weight: bold">
							{{ index + 1 }}
						</view>
						<text class="text padding-left-lg text-red">
							{{item.dlid.cdlcode}}
						</text>
					</view>
					<view class="">
						<!-- :disabled="item.planWeight?false:true"  -->
						<u-checkbox v-model="item.checked" :size="50" :key="index"
							@change="addData(item)"></u-checkbox>
					</view>
				</view>
				<!-- <view class="text padding-left-lg text-red"><text class="">发货单号：</text>{{item.dlid.cdlcode}}</view>
				<br /> -->
				<view class="text padding-left-lg"><text class="">日期：</text>{{item.dlid.ddate}}</view><br />
				<view class="text padding-left-lg"><text class="">客户：</text>{{item.dlid.ccusname}}</view><br />
				<view class="text padding-left-lg"><text class="">合同：</text>{{item.ccontractid}}</view><br />
				<view class="text padding-left-lg"><text class="">仓库：</text>{{item.cwhname}}</view><br />
				<view class="text padding-left-lg"><text class="">批次：</text>{{item.ibatch}}</view><br />
				<view class="text padding-left-lg"><text class="">存货：</text>{{item.cinvname}}</view><br />
				<view class="text padding-left-lg"><text class="">规格：</text>{{item.cinvstd}}</view><br />
				<view class="text padding-left-lg"><text class="">数量：</text>{{item.qty}}</view><br />
				<view class="padding-left-lg" style="margin-top: -15px;">
					<u-form-item label="计划发货重量:" prop="planWeight" label-width="280" required
						:label-style="{'font-weight':'bold','margin-left':'16px'}">
						<u-input placeholder="请输入" v-model="item.planWeight" type="digit" class="text-red" di
							maxlength="64" @input="planWeightChange($event,item)"></u-input>
					</u-form-item>
				</view>
				<view class="padding-left-lg" style="margin-top: -15px;">
					<u-form-item label="承运单价:" prop="cyPrice" label-width="280" required
						:label-style="{'font-weight':'bold','margin-left':'16px'}">
						<u-input placeholder="请输入" v-model="item.cyPrice" type="digit" class="text-red" di
							maxlength="64" @input="cyPriceChange($event,item)"></u-input>
					</u-form-item>
				</view>
				<view class="padding-left-lg" style="margin-top: -15px;">
					<u-form-item label="承运目的地:" prop="cyAddress" label-width="280" required
						:label-style="{'font-weight':'bold','margin-left':'16px'}">
						<u-input placeholder="请输入" v-model="item.cyAddress" type="text" class="text-red" di
							maxlength="64" @input="cyAddressChange($event,item)"></u-input>
					</u-form-item>
				</view>
				
			</view>
			<view class="floatright">
				<u-button type="primary" @click="toAdd">确定</u-button>
			</view>
			<view class="loadmore">
				<u-loadmore :status="loadStatus"></u-loadmore>
			</view>
		</scroll-view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				carType:'',
				address: [],
				provinceList: [],
				cityAllList: [],
				addressIndex: [0, 0],
				keywords: '',
				query: {
					status: '',
					pageNo: 1,
					pageSize: 20,
					dlid: {},
				},
				list: [],
				hid: '',
				fhList: [],
				addList: [],
				loadStatus: 'loadmore',
				show: false,
				type: 'text',
				startTime: false, //控制日期显示
				endTime: false, //控制日期显示
			}
		},
		onLoad(params) {
			this.carType = params.carType
			this.hid = params.hid
			this.loadList();
		},
		methods: {
			cyPriceChange(e, item) {
				item.cyPrice = e
				this.$forceUpdate()
			},
			cyAddressChange(e, item) {
				item.cyAddress = e
				this.$forceUpdate()
			},
			planWeightChange(e, item) {
				console.log(this.carType,'this.carType====');
				if (this.carType == 1) {
					if (e > 0) {
						e = -e
					}
				} else {
					if (e < 0) {
						e = -e
					}
				}
				item.planWeight = e
				this.$forceUpdate()
			
			},
			addData(item) {

				var addList = this.addList
				for (var i = 0; i < addList.length; i++) {
					if (addList[i].id == item.id) {
						addList.splice(i, 1);
					}
				}
				if (item.checked == false || item.checked == undefined) {
					addList.push(item)
				}

			},
			toAdd() {
				if (this.addList.length > 0) {
					var data = {
						dispatchlists: JSON.stringify(this.addList),
						hid: this.hid,
						useStatus: 1
					};
					let flag = true
					let cyPriceFlag = true
					let cyAddressFlag = true
					this.addList.forEach(item=>{
						if(!item.planWeight || item.planWeight <= 0){
							flag=false
						}
						if(!item.cyPrice || item.cyPrice <= 0){
							cyPriceFlag = false
						}
						if(!item.cyAddress){
							cyAddressFlag = false
						}
					})
					
					if(!flag || !cyPriceFlag || !cyAddressFlag){
						// this.$u.toast("请填写计划发货重量后重新确定！");
						this.$refs.jsError.showError('',"请填写计划发货重量，承运单价，承运目的地后重新确定！",'error');
					}else{
						this.$u.api.mffh.addData(data).then(res => {
							if(res.result == "true"){
								this.$u.toast(res.message);
								setTimeout(()=>{
									uni.$emit('refreshData');
									uni.navigateBack({
										delta: 1,
									})
								},1000)
								
							}else{
								this.$refs.jsError.showError('',res.message,'error');
							}
						})
					}
				} else {
					// this.$u.toast("请选择至少一条数据！");
					this.$refs.jsError.showError('',"请选择至少一条数据！",'error');
					return;
				}
			},
			loadMore() {
				this.loadStatus = "loading";
				setTimeout(() => {
					this.query.pageNo += 1;
					this.loadList("add");
				}, 100);
			},
			loadList(type) {
				var data = {
					dispatchlists: JSON.stringify(this.query),
					useStatus: 1
				};
				this.$u.api.mffh.fhList(data).then(res => {
					var data = res.list;
					if (type == "add") {
						for (var i = 0; i < data.length; i++) {
							this.fhList.push(data[i])
						}
					} else {
						this.fhList = data;
					}
					if (res.list.length >= res.count || res.list.length < 20) {
						this.loadStatus = "nomore";
					}
				})
			},
			startConfirm(e) {
				this.query.ddate_gte = e.year + '-' + e.month + '-' + e.day;
			},
			endConfirm(e) {
				this.query.ddate_lte = e.year + '-' + e.month + '-' + e.day;
			},
			optionsClick(rowIndex, btnIndex) {
				let row = this.list[rowIndex];
				if (btnIndex == 0) {
					bpmUtils.navTrace(this, 'formKey=leave&bizKey=' + row.id);
				}
			},
			navTo(url) {
				uni.navigateTo({
					url: url
				});
			},
			submit() {
				this.fhList = [];
				this.loadList();
				this.show = false
			},
			reset() {
				this.query.ddate_gte = ''
				this.query.ddate_lte = ''
				this.query.dlid = {}
				this.show = false
				this.loadList();
			},
		}
	}
</script>

<style  lang="less">
	.tableclass {
		border-left: 1px solid rgb(228, 231, 237);
		border-top: 1px solid rgb(228, 231, 237);
		background-color: rgb(255, 255, 255);
		width: 100%;
		box-sizing: border-box;
	}

	.thclass {
		text-align: center;
		padding: 5px 3px;
		border-bottom: 1px solid rgb(228, 231, 237);
		border-right: 1px solid rgb(228, 231, 237);
		flex-direction: column;
		flex: 1;
		justify-content: center;
		font-size: 14px;
		color: #303133;
		font-weight: bold;
		background-color: #f5f6f8;
	}

	.trclass {
		height: 52px;
	}

	.tdclass {
		padding: 5px 3px;
		border-bottom: 1px solid rgb(228, 231, 237);
		border-right: 1px solid rgb(228, 231, 237);
		flex-direction: column;
		flex: 1;
		justify-content: center;
		font-size: 14px;
		color: #505256;
		align-self: stretch;
		box-sizing: border-box;
		height: 100%;
	}

	.box .item {
		margin: 0 0px 0px;
	}

	.tui-line-cell {
		width: 100%;
		box-sizing: border-box;
		display: flex;
		align-items: center;
	}

	.tui-title {
		line-height: 32rpx;
		min-width: 120rpx;
		flex-shrink: 0;
	}

	.tui-input {
		font-size: 32rpx;
		color: #333;
		padding-left: 20rpx;
		flex: 1;
		overflow: visible;
	}

	/* .footer {
	position: fixed;
	left: 0;
	bottom: 20px;
	width: 100%;
	
} */
	.floatright {
		position: fixed;
		bottom: 12px;
		right: 12px;
		width: 25%;
		line-height: var(--footer-height);
		background: #ffffff;
		color: #ffffff;
	}

	.add {
		position: absolute;
		right: 130rpx;
		top: 0;
		margin-right: 0;
	}

	.upload {
		position: absolute;
		right: 65rpx;
		top: 0;
		margin-right: 0;
	}

	.rights {
		position: absolute;
		/* display: inline-block; */
		right: 0;
		top: 0;
		margin-right: 0;
		/* width: 100%; */

	}

	.cu-btn {
		font-size: 16px;
		height: 40px;
	}

	.foot {
		background: #fff;
	}

	.cu-item {
		flex-wrap: wrap;
		padding: 8px 15px !important;
	}

	.title {
		width: 220rpx;
	}

	.cu-modal-slot {
		margin: 42rpx 32rpx 20rpx 32rpx;
		height: 100vh;
		position: relative;
	}

	.cu-modal-header {
		font-size: 38rpx;
		color: #3d3d3d;
		line-height: 40rpx;
		text-align: left;
		font-weight: 500;
	}

	.button {
		font-size: 32rpx;
		color: #666666;
		line-height: 40rpx;
		padding: 12rpx 40rpx;
		margin-bottom: 20rpx;
		background: #e2e2e2;
		border-radius: 180rpx;
	}

	.button:hover {
		background: #3e97b0;
		color: #ffffff;
	}

	.cu-modal-search-title {
		margin: 32rpx 0rpx 20rpx 0rpx;
		text-align: left;
		font-size: 32rpx;
		color: #999999;
		line-height: 40rpx;
	}

	.cu-dialog {
		background: #ffffff;
	}

	.cu-modal-footer {
		padding: 32rpx 32rpx !important;
		width: 100% !important;

		.cu-btn {
			width: 50% !important;
		}
	}

	.bg-confirm {
		background: #3e97b0;
		color: #ffffff;
	}
</style>